# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time        : 2021/10/28 20:56
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : opencv_segment.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
import cv2 as cv
from PIL import Image
from DIP_experiment_5.util import contrast_show, histogram
im_path = "../experiment_fig/local_threshold .tif"
font_path = "C:/Windows/Fonts/simhei.ttf"
im_arr = cv.imread(im_path)
b, g, r = cv.split(im_arr)
r_im = Image.fromarray(r)
im_dict = {"原始图像r通道": r_im}
histogram(im_dict, (1, 1), font=font_path)
valley_gray = [30, 55]
images_dict = {}
for val in valley_gray:
    _, dst = cv.threshold(r, thresh=val, maxval=255, type=cv.THRESH_BINARY)
    images_dict[f"r通道基本阈值={val}分割"] = dst
for c in range(1, 5):
    c = 1 << c
    dst = cv.adaptiveThreshold(r, maxValue=255, adaptiveMethod=cv.ADAPTIVE_THRESH_MEAN_C,
                               thresholdType=cv.THRESH_BINARY, blockSize=3, C=c)
    images_dict[f"r通道自适应阈值分割C={c}"] = dst
contrast_show(images_dict, (2, 3), wspace=0.4,font=font_path)
